<?php
namespace app\common\component\help_get;

use think\Session;

class HelpGetService {
	public function create($data) {
		$data['order_id'] = 'GET' . date('YmdHis') . crypto_rand(100, 999);
		$helpGetModel = new HelpGetModel($data);

    	if($helpGetModel -> save()) {
    		return [
    			'code' => 0,
    			'message' => '提现成功!',
    			'data' => [
    				'help_get' => $helpGetModel
    			]
    		];
    	}

    	return [
			'code' => -1,
			'message' => '提现失败!'
		];
	}
	
	public function update($data, $help_get_id) {
		$updateHelpGet = (new HelpGetModel()) -> where('help_get_id', $help_get_id) -> update($data);

    	if($updateHelpGet) {
    		return [
    			'code' => 0,
    			'message' => '提现修改成功'
    		];
    	}

    	return [
			'code' => -1,
			'message' => '提现修改失败'
		];
	}

	public function withdraw($money) {
		$userId = Session::get('user_id');
		$post['help_to_id'] = 0;
    	$post['user_id'] = $userId;
    	$post['money'] = $money;
    	$post['begin_time'] = $post['create_time'] = time();
    	$post['status'] = 0;
    	$getLastHelpGet = HelpGetModel::where('status = 0 AND user_id = ' . $userId) -> order('sort DESC') -> find();
    	if($getLastHelpGet) {
    		$post['sort'] = $getLastHelpGet -> sort + 1;
    	}

		return $this -> create($post);
	}

	public function getHelpGetAndHelpToJoins($status = null, $page = 10) {
		$helpToModel = (new HelpGetModel()) -> alias('h_g')
		-> join('league_help_to h_t', 'h_t.help_to_id = h_g.help_to_id')
		-> join('league_user u', 'h_t.user_id = u.user_id')
		-> field(
			'h_g.help_get_id, h_g.help_to_id, h_g.user_id h_g_user_id, h_g.order_id, h_g.money, h_g.create_time, h_g.begin_time, h_g.status, ' .
			'h_t.pic, ' .
			'u.username, u.realname, u.phone, u.bank_code'
		)
		-> where('h_g.user_id', Session::get('user_id'));

		if(!is_null($status)) {
			$helpToModel -> where('h_g.status', $status);
		}

		return $helpToModel -> order('h_g.status DESC, h_g.help_get_id ASC') -> paginate($page, true);
	}

	public function getHelpGets($page = 10) {
		return (new HelpGetModel()) -> where('user_id', Session::get('user_id')) -> order('status ASC, help_get_id ASC') -> paginate($page, true);
	}

	public function getAll($page = 10) {
		return (new HelpGetModel()) -> alias('h_g')
			-> join('league_help_to h_t', 'h_t.help_to_id = h_g.help_to_id')
			-> join('league_user u', 'h_g.user_id = u.user_id')
			-> join('league_user pu', 'h_t.user_id = pu.user_id')
			-> field(
				'h_g.help_get_id, h_g.help_to_id, h_g.user_id, h_g.order_id, h_g.money, h_g.create_time, h_g.begin_time, h_g.status, ' .
				'u.username, u.realname, u.phone, u.bank_code, ' .
				'pu.username p_username, pu.realname p_realname, pu.bank_code p_bank_code'
			)
			-> order('h_g.status ASC, h_g.sort ASC, h_g.help_get_id DESC')
			-> paginate($page);
	}

	public function getHelpGet($help_get_id) {
		$getHelpGet = HelpGetModel::get($help_get_id);
		if($getHelpGet) {
			return [
				'code' => 0,
				'message' => 'Success',
				'data' => [
					'help_get' => $getHelpGet
				]
			];
		}

		return [
			'code' => -1,
			'message' => '提现订单不存在!'
		]; 
	}
}
?>